/**
 * app.js
 * create by ben. 201604220
 */
!(function () {

    var Utils = {
        Regex:{
            mobile: /^1((3|5|8){1}\d{1}|70)\d{8}$/,
            email: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
        },
        /**
         * preload imgs
         * @param imageUrls
         * @param progress
         * @param complete
         */
        loadImages:function (imageUrls, progress, complete) {
            var loader = new PxLoader();
            for( var i=0;i<imageUrls.length;i++){
                loader.add(new PxLoaderImage(imageUrls[i]));
            }
            loader.addProgressListener(progress);
            loader.addCompletionListener(complete);
            loader.start();
        },
        /**
         * 防止高频调用
         * @param func
         * @param wait
         * @param immediate
         * @returns {Function}
         */
        debounce : function(func, wait, immediate) {
            var timeout;
            return function() {
                var context = this, args = arguments;
                var later = function() {
                    timeout = null;
                    if (!immediate) func.apply(context, args);
                };
                var callNow = immediate && !timeout;
                clearTimeout(timeout);
                timeout = setTimeout(later, wait);
                if (callNow) func.apply(context, args);
            };
        },
        /**
         * 打印
         * @param str
         * @returns {*}
         */
        sprintf : function (str) {
            var args = arguments,
                flag = true,
                i = 1;
            str = str.replace(/%s/g, function () {
                var arg = args[i++];
                if (typeof arg === 'undefined') {
                    flag = false;
                    return '';
                }
                return arg;
            });
            return flag ? str : '';
        },
        getSearchParams:function () {
            var match,
                urlParams = {},
                pl        = /\+/g,  // Regex for replacing addition symbol with a space
                search    = /([^&=]+)=?([^&]*)/g,
                decode    = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
                query     = window.location.search.substring(1);
            while (match = search.exec(query))
                urlParams[decode(match[1])] = decode(match[2]);
            return urlParams;
        },
        browser:{
            versions:function(){
                var u = navigator.userAgent, app = navigator.appVersion;
                return {//移动终端浏览器版本信息
                    trident: u.indexOf('Trident') > -1, //IE内核
                    presto: u.indexOf('Presto') > -1, //opera内核
                    webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                    gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
                    mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                    ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                    android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
                    iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器
                    iPad: u.indexOf('iPad') > -1, //是否iPad
                    webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
                };
            }(),
            language:(navigator.browserLanguage || navigator.language).toLowerCase()
        },
        request: function(name) {
            return this.getSearchParams()[name];
        }
    };

    window.Utils = Utils;
})();